###################################
# Title: The ‘Tough-on-Crime’ Left: Race, Gender, and
######## Elections of Law-and-Order Democrats
# Authors: Isabel Laterzo-Tingley & Leah Christiani
# Date: May 2024

# File: Appendix 2 -- Additional Survey Results
#####################################

#clear global environ
rm(list=ls())

#packages
library(tidyverse)
library(naniar)
library(cregg)
library(ggplot2)
library(scales)

#working rd
setwd("/Users/IsabelLaterzo/Dropbox/Research Projects/Tough-on-Crime, Race, Gender Paper - L. Christiani and I. Laterzo/Data/Replication Archive")

#read in data
data <- readRDS("survey1_data.rds")


#############################################################################
############################### DATA SET UP ################################
#############################################################################

# total respondents
total_n <- data %>% 
  select(id) %>%
  unique()

# Isolate Democrats/Democrat Leaners
dem_data <- data %>% filter(dems == 1)

#respondents?
dem_total <- dem_data %>%
  select(id) %>%
  unique()

# Isolate Republicans/Republican Leaners
rep_data <- data %>% filter(reps == 1)

#respondents?
rep_total <- rep_data %>%
  select(id) %>%
  unique()

#create subset of data for white respondents
whiteresp_data <- data %>% filter(race_white == 1)

#create subset of data for Black respondents
blackresp_data <- data %>% filter(race_black == 1)

# create subset of data for female-identifying respondents
female_data <- data %>% filter(female == 1)

# create subset of data for non-female-identifying respondents
male_data <- data %>% filter(female == 0)


# Isolate true independents
ind_data <- data %>% filter(dems == 0 & reps == 0)

#respondents? 256
ind_total <- ind_data %>%
  select(id) %>%
  unique()


# Isolate strong democrats
strongdem_data <- data %>% filter(pid == "Democrat")

#respondents? 552
strongdem_total <- strongdem_data %>%
  select(id) %>%
  unique()

# Isolate lean democrats
leandem_data <- data %>% filter(pidLean == "Democrat")

#respondents? 552
leandem_total <- leandem_data %>%
  select(id) %>%
  unique()

# Isolate those who believe tough-on-crime policies target black individuals
pooled_data_target <- data %>%
  filter(tough_racist >= 5)

#respondents?
pool_target_total <- pooled_data_target %>%
  select(id) %>%
  unique()

pooled_data_notarget <- data %>%
  filter(tough_racist <= 3)

#respondents?
pool_notarget_total <- pooled_data_notarget %>%
  select(id) %>%
  unique()


## isolate them among democrats
dem_data_target <- dem_data %>%
  filter(tough_racist > 3)

#respondents?
dem_target_total <- dem_data_target %>%
  select(id) %>%
  unique()

dem_data_notarget <- dem_data %>%
  filter(tough_racist < 3)

#respondents?
dem_target_nototal <- dem_data_notarget %>%
  select(id) %>%
  unique()



################################################################################
############################# CONJOINT RESULTS #################################
################################################################################


# Figure A9: Pooled Respondent Preferences for All Candidate Attributes

base_model <- cj(data,
                 chosen ~ cand_gender + cand_race + cand_tax + cand_pubsec + cand_edu,
                 id = ~id,
                 estimate = "mm",
                 alpha = .1)


dem_model <- cj(dem_data,
                chosen ~ cand_gender + cand_race + cand_tax + cand_pubsec + cand_edu,
                id = ~id,
                estimate = "mm",
                alpha = .1)

rep_model <- cj(rep_data,
                chosen ~ cand_gender + cand_race + cand_tax + cand_pubsec + cand_edu,
                id = ~id,
                estimate = "mm",
                alpha = .1)

## combining
base_model$sample <- "Pooled"
dem_model$sample <- "Democrats"
rep_model$sample <- "Republicans"
all_pooled <- rbind(base_model, dem_model, rep_model)
all_pooled$sample <- factor(all_pooled$sample, 
                            levels = c("Pooled", "Democrats", "Republicans"),
                            ordered = T)

#re-label features for plot
all_pooled$feature <- ifelse(all_pooled$feature == "cand_gender", "Candidate Gender",
                             ifelse(all_pooled$feature == "cand_race", "Candidate Race",
                                    ifelse(all_pooled$feature == "cand_tax", "Tax Policy",
                                           ifelse(all_pooled$feature == "cand_pubsec", "Public Security Policy",
                                                  ifelse(all_pooled$feature == "cand_edu", "Education Policy", NA)))))


fig9_app <- ggplot(all_pooled, aes(x = level, y = estimate,color = feature)) +
  geom_hline(yintercept = 0.5, colour = gray(0.7), lty = 1) + 
  geom_point(position=position_dodge(0.2)) +
  geom_errorbar(aes(x = level, 
                    ymin = lower,
                    ymax = upper),
                width = .4,
                alpha = 0.8,
                position=position_dodge(0.2),
                lwd = 0.7) +
  facet_wrap(~sample) +
  coord_flip() +
  scale_linetype_discrete(name="Candidate Race",
                          labels =c("Black", "White")) +
  labs(x = "Policy",
       y = "Marginal Mean",
       color = "Attribute") +
  scale_y_continuous(labels = label_number(accuracy = 0.01),
                     breaks = c(0.48, 0.5, 0.52)) +
  theme_bw() +
  theme(text = element_text(size = 16),
        axis.text.x = element_text(size = 12),
        axis.text.y = element_text(size = 14),
        # legend.position = "bottom",
        legend.box.background = element_rect(colour = "black"),
        panel.spacing = unit(2, "lines"))

fig9_app

#ggsave("app_base_all_plot.png", fig9_app, width=12, height=5)




# Figure A10: Pooled Respondent Preferences for All Candidate Attributes 
############# by Candidate Race

base_model_race <- cj(data,
                      chosen ~ cand_gender + cand_tax + cand_pubsec + cand_edu,
                      id = ~id,
                      by = ~cand_race,
                      estimate = "mm",
                      alpha = .1)


dem_model_race <- cj(dem_data,
                     chosen ~ cand_gender + cand_tax +
                       cand_pubsec + cand_edu,
                     id = ~id,
                     by = ~cand_race,
                     estimate = "mm",
                     alpha = .1)

rep_model_race <- cj(rep_data,
                     chosen ~ cand_gender + cand_tax +
                       cand_pubsec + cand_edu,
                     id = ~id,
                     by = ~cand_race,
                     estimate = "mm",
                     alpha = .1)


### combining
base_model_race$sample <- "Pooled"
dem_model_race$sample <- "Democrats"
rep_model_race$sample <- "Republicans"
race_all_pooled <- rbind(base_model_race, dem_model_race, rep_model_race)
race_all_pooled$sample <- factor(race_all_pooled$sample, 
                                 levels = c("Pooled", "Democrats", "Republicans"),
                                 ordered = T)

#re-label features for plot
race_all_pooled$feature <- ifelse(race_all_pooled$feature == "cand_gender", "Candidate Gender",
                                  ifelse(race_all_pooled$feature == "cand_race", "Candidate Race",
                                         ifelse(race_all_pooled$feature == "cand_tax", "Tax Policy",
                                                ifelse(race_all_pooled$feature == "cand_pubsec", "Public Security Policy",
                                                       ifelse(race_all_pooled$feature == "cand_edu", "Education Policy", NA)))))


fig10_app <- ggplot(race_all_pooled, aes(x = level, y = estimate, linetype = cand_race, color = feature)) +
  geom_hline(yintercept = 0.5, colour = gray(0.7), lty = 1) + 
  geom_point(position=position_dodge(0.2)) +
  geom_errorbar(aes(x = level, 
                    ymin = lower,
                    ymax = upper,
                    linetype = BY),
                width = .4,
                alpha = 0.8,
                position=position_dodge(0.2),
                lwd = 0.7) +
  facet_wrap(~sample) +
  coord_flip() +
  scale_linetype_discrete(name="Candidate Race",
                          labels =c("Black", "White")) +
  labs(x = "Policy",
       y = "Marginal Mean",
       color = "Attribute") +
  theme_bw() +
  theme(text = element_text(size = 16),
        axis.text.x = element_text(size = 12),
        axis.text.y = element_text(size = 14),
        # legend.position = "bottom",
        legend.box.background = element_rect(colour = "black"),
        panel.spacing = unit(2, "lines"))

fig10_app

#ggsave("app_race_all_plot.png", fig10_app, width=12, height=5)





# Figure A11: Pooled Respondent Preferences for All Candidate Attributes 
############# by Candidate Gender

base_model_gender <- cj(data,
                        chosen ~ cand_race + cand_tax + cand_pubsec + cand_edu,
                        id = ~id,
                        by = ~cand_gender,
                        estimate = "mm",
                        alpha = .1)


dem_model_gender <- cj(dem_data,
                       chosen ~ cand_race + cand_tax +
                         cand_pubsec + cand_edu,
                       id = ~id,
                       by = ~cand_gender,
                       estimate = "mm",
                       alpha = .1)

rep_model_gender <- cj(rep_data,
                       chosen ~ cand_race + cand_tax +
                         cand_pubsec + cand_edu,
                       id = ~id,
                       by = ~cand_gender,
                       estimate = "mm",
                       alpha = .1)


### combining
base_model_gender$sample <- "Pooled"
dem_model_gender$sample <- "Democrats"
rep_model_gender$sample <- "Republicans"
gender_all_pooled <- rbind(base_model_gender, dem_model_gender, rep_model_gender)
gender_all_pooled$sample <- factor(gender_all_pooled$sample, 
                                   levels = c("Pooled", "Democrats", "Republicans"),
                                   ordered = T)

#re-label features for plot
gender_all_pooled$feature <- ifelse(gender_all_pooled$feature == "cand_gender", "Candidate Gender",
                                    ifelse(gender_all_pooled$feature == "cand_race", "Candidate Race",
                                           ifelse(gender_all_pooled$feature == "cand_tax", "Tax Policy",
                                                  ifelse(gender_all_pooled$feature == "cand_pubsec", "Public Security Policy",
                                                         ifelse(gender_all_pooled$feature == "cand_edu", "Education Policy", NA)))))


fig11_app <- ggplot(gender_all_pooled, aes(x = level, y = estimate, linetype = cand_gender, color = feature)) +
  geom_hline(yintercept = 0.5, colour = gray(0.7), lty = 1) + 
  geom_point(position=position_dodge(0.2)) +
  geom_errorbar(aes(x = level, 
                    ymin = lower,
                    ymax = upper,
                    linetype = BY),
                width = .4,
                alpha = 0.8,
                position=position_dodge(0.2),
                lwd = 0.7) +
  facet_wrap(~sample) +
  coord_flip() +
  scale_linetype_discrete(name="Candidate Gender",
                          labels =c("Female", "Male")) +
  labs(x = "Policy",
       y = "Marginal Mean",
       color = "Attribute") +
  theme_bw() +
  theme(text = element_text(size = 16),
        axis.text.x = element_text(size = 12),
        axis.text.y = element_text(size = 14),
        # legend.position = "bottom",
        legend.box.background = element_rect(colour = "black"),
        panel.spacing = unit(2, "lines"))

fig11_app

#ggsave("app_gender_all_plot.png", fig11_app, width=12, height=5)




# Figure A12: Preferences by Respondent Gender for All Candidate Attributes


base_model_female <- cj(female_data,
                        chosen ~ cand_gender + cand_race + cand_tax +
                          cand_pubsec + cand_edu,
                        id = ~id,
                        estimate = "mm",
                        alpha = .1)


base_model_male <- cj(male_data,
                      chosen ~ cand_gender + cand_race + cand_tax +
                        cand_pubsec + cand_edu,
                      id = ~id,
                      estimate = "mm",
                      alpha = .1)


### combining
base_model_female$sample <- "Female"
base_model_male$sample <- "Male"
base_gender_pooled <- rbind(base_model_female, base_model_male)
base_gender_pooled$sample <- factor(base_gender_pooled$sample, 
                                    levels = c("Female", "Male"),
                                    ordered = T)



base_gender_pooled$feature <- ifelse(base_gender_pooled$feature == "cand_gender", "Candidate Gender",
                                     ifelse(base_gender_pooled$feature == "cand_race", "Candidate Race",
                                            ifelse(base_gender_pooled$feature == "cand_tax", "Tax Policy",
                                                   ifelse(base_gender_pooled$feature == "cand_pubsec", "Public Security Policy",
                                                          ifelse(base_gender_pooled$feature == "cand_edu", "Education Policy", NA)))))



fig12_app <- ggplot(base_gender_pooled, aes(x = level, y = estimate, color = feature)) +
  geom_hline(yintercept = 0.5, colour = gray(0.7), lty = 1) + 
  geom_point(position=position_dodge(0.2)) +
  geom_errorbar(aes(x = level, 
                    ymin = lower,
                    ymax = upper),
                width = .3,
                alpha = 0.8,
                position=position_dodge(0.2),
                lwd = 0.7) + 
  facet_wrap(~sample) +
  coord_flip() +
  scale_y_continuous(labels = label_number(accuracy = 0.01),
                     breaks = c(0.48, 0.5, 0.52)) +
  labs(x = "Crime Policy",
       y = "Marginal Mean",
       color = "Attribute") +
  theme_bw() +
  theme(text = element_text(size = 12),
        axis.text.x = element_text(size = 12),
        axis.text.y = element_text(size = 12),
        legend.box.background = element_rect(colour = "black"),
        panel.spacing = unit(2, "lines"))

fig12_app

#ggsave("app_fig2_plot.png", fig12_app, width=8, height=5)



# Figure A13: Preferences by Respondent Race for All Candidate Attributes

base_model_black <- cj(blackresp_data,
                       chosen ~ cand_gender + cand_race + cand_tax +
                         cand_pubsec + cand_edu,
                       id = ~id,
                       estimate = "mm",
                       alpha = .1)


base_model_white <- cj(whiteresp_data,
                       chosen ~ cand_gender + cand_race + cand_tax +
                         cand_pubsec + cand_edu,
                       id = ~id,
                       estimate = "mm",
                       alpha = .1)


### combining
base_model_black$sample <- "Black"
base_model_white$sample <- "White"
base_race_pooled <- rbind(base_model_black, base_model_white)
base_race_pooled$sample <- factor(base_race_pooled$sample, 
                                  levels = c("Black", "White"),
                                  ordered = T)



base_race_pooled$feature <- ifelse(base_race_pooled$feature == "cand_gender", "Candidate Gender",
                                   ifelse(base_race_pooled$feature == "cand_race", "Candidate Race",
                                          ifelse(base_race_pooled$feature == "cand_tax", "Tax Policy",
                                                 ifelse(base_race_pooled$feature == "cand_pubsec", "Public Security Policy",
                                                        ifelse(base_race_pooled$feature == "cand_edu", "Education Policy", NA)))))



fig13_app <- ggplot(base_race_pooled, aes(x = level, y = estimate, color = feature)) +
  geom_hline(yintercept = 0.5, colour = gray(0.7), lty = 1) + 
  geom_point(position=position_dodge(0.2)) +
  geom_errorbar(aes(x = level, 
                    ymin = lower,
                    ymax = upper),
                width = .3,
                alpha = 0.8,
                position=position_dodge(0.2),
                lwd = 0.7) + 
  facet_wrap(~sample) +
  coord_flip() +
  scale_y_continuous(labels = label_number(accuracy = 0.01),
                     breaks = c(0.45, 0.5, 0.55)) +
  labs(x = "Crime Policy",
       y = "Marginal Mean",
       color = "Attribute") +
  theme_bw() +
  theme(text = element_text(size = 12),
        axis.text.x = element_text(size = 12),
        axis.text.y = element_text(size = 12),
        legend.box.background = element_rect(colour = "black"),
        panel.spacing = unit(2, "lines"))

fig13_app

#ggsave("app_fig3_plot.png", fig13_app, width=8, height=5)







# Figure A14: Pooled Respondent Preferences for Crime Platforms Across Full 
############ Sample and Declared Independents


base_model <- cj(data,
                 chosen ~ cand_pubsec,
                 id = ~id,
                 estimate = "mm",
                 alpha = .1)

ind_model <- cj(ind_data,
                chosen ~ cand_pubsec,
                id = ~id,
                estimate = "mm",
                alpha = .1)


### combining
base_model$sample <- "Pooled"
ind_model$sample <- "Independents"
base_pooled <- rbind(base_model, ind_model)
base_pooled$sample <- factor(base_pooled$sample, 
                             levels = c("Pooled", "Independents"),
                             ordered = T)


fig14_app <- ggplot(base_pooled, aes(x = level, y = estimate)) +
  geom_hline(yintercept = 0.5, colour = gray(0.7), lty = 1) + 
  geom_point(position=position_dodge(0.2)) +
  geom_errorbar(aes(x = level, 
                    ymin = lower,
                    ymax = upper),
                width = .3,
                alpha = 0.8,
                position=position_dodge(0.2),
                lwd = 0.7) + 
  facet_wrap(~sample) +
  coord_flip() +
  scale_y_continuous(labels = label_number(accuracy = 0.01),
                     breaks = c(0.48, 0.5, 0.52)) +
  labs(x = "Crime Policy",
       y = "Marginal Mean") +
  theme_bw() +
  theme(text = element_text(size = 16),
        axis.text.x = element_text(size = 14),
        axis.text.y = element_text(size = 14),
        legend.position = "bottom",
        legend.box.background = element_rect(colour = "black"),
        panel.spacing = unit(2, "lines"))

fig14_app

#ggsave("app_ind_fig4_plot.png", fig14_app, width=8, height=3)






# Figure A15: Pooled Respondent Preferences for Crime Platforms Across Full 
############ Sample and Declared Independents by Candidate Race

base_interact_race <- cj(data,
                         chosen ~ cand_pubsec,
                         id = ~id,
                         estimate = "mm",
                         by = ~ cand_race,
                         alpha = .1)

ind_interact_race <- cj(ind_data,
                        chosen ~ cand_pubsec,
                        id = ~id,
                        estimate = "mm",
                        by = ~ cand_race,
                        alpha = .1)


### combining
base_interact_race$sample <- "Pooled"
ind_interact_race$sample <- "Independents"
race_pooled <- rbind(base_interact_race, ind_interact_race)
race_pooled$sample <- factor(race_pooled$sample, 
                             levels = c("Pooled", "Independents"),
                             ordered = T)



fig15_app <- ggplot(race_pooled, aes(x = level, y = estimate, linetype = cand_race)) +
  geom_hline(yintercept = 0.5, colour = gray(0.7), lty = 1) + 
  geom_point(position=position_dodge(0.2)) +
  geom_errorbar(aes(x = level, 
                    ymin = lower,
                    ymax = upper,
                    linetype = BY),
                width = .3,
                alpha = 0.8,
                position=position_dodge(0.2),
                lwd = 0.7) +
  facet_wrap(~sample) +
  coord_flip() +
  scale_y_continuous(labels = label_number(accuracy = 0.01),
                     breaks = c(0.47, 0.5, 0.53)) +
  scale_linetype_discrete(name="Candidate Race",
                          labels =c("Black", "White")) +
  labs(x = "Crime Policy",
       y = "Marginal Mean") +
  theme_bw() +
  theme(text = element_text(size = 16),
        axis.text.x = element_text(size = 14),
        axis.text.y = element_text(size = 14),
        legend.position = "bottom",
        legend.box.background = element_rect(colour = "black"),
        panel.spacing = unit(2, "lines"))

fig15_app 

#ggsave("app_ind_fig5_plot.png", fig15_app, width=8, height=3)



# Figure A16: Pooled Respondent Preferences for Crime Platforms Across Full 
############ Sample and Declared Independents by Candidate Gender

base_interact_gender <- cj(data,
                           chosen ~ cand_pubsec,
                           id = ~id,
                           estimate = "mm",
                           by = ~ cand_gender,
                           alpha = .1)

ind_interact_gender <- cj(ind_data,
                          chosen ~ cand_pubsec,
                          id = ~id,
                          estimate = "mm",
                          by = ~ cand_gender,
                          alpha = .1)


### combining
base_interact_gender$sample <- "Pooled"
ind_interact_gender$sample <- "Independents"
gender_pooled <- rbind(base_interact_gender, ind_interact_gender)
gender_pooled$sample <- factor(gender_pooled$sample, 
                               levels = c("Pooled", "Independents"),
                               ordered = T)



fig16_app <- ggplot(gender_pooled, aes(x = level, y = estimate, linetype = cand_gender)) +
  geom_hline(yintercept = 0.5, colour = gray(0.7), lty = 1) + 
  geom_point(position=position_dodge(0.2)) +
  geom_errorbar(aes(x = level, 
                    ymin = lower,
                    ymax = upper,
                    linetype = BY),
                width = .3,
                alpha = 0.8,
                position=position_dodge(0.2),
                lwd = 0.7) +
  facet_wrap(~sample) +
  coord_flip() +
  scale_y_continuous(labels = label_number(accuracy = 0.01),
                     breaks = c(0.47, 0.5, 0.53)) +
  scale_linetype_discrete(name="Candidate Gender",
                          labels =c("Female", "Male")) +
  labs(x = "Crime Policy",
       y = "Marginal Mean") +
  theme_bw() +
  theme(text = element_text(size = 16),
        axis.text.x = element_text(size = 14),
        axis.text.y = element_text(size = 14),
        legend.position = "bottom",
        legend.box.background = element_rect(colour = "black"),
        panel.spacing = unit(2, "lines"))

fig16_app

#ggsave("app_ind_fig6_plot.png", fig16_app, width=8, height=3)




# Figure A17: Preferences for Crime Platforms by Candidate Race and Gender 
############ Among Declared Independents 

black_ind_cands <- ind_data %>% filter(cand_race == "Black")
white_ind_cands <- ind_data %>% filter(cand_race == "White")

ind_interact_genblack <- cj(black_ind_cands,
                            chosen ~ cand_pubsec,
                            id = ~id,
                            estimate = "mm",
                            by = ~ cand_gender,
                            alpha = .1)

ind_interact_genwhite <- cj(white_ind_cands,
                            chosen ~ cand_pubsec,
                            id = ~id,
                            estimate = "mm",
                            by = ~ cand_gender,
                            alpha = .1)

### combining
ind_interact_genblack$sample <- "Candidate: Black"
ind_interact_genwhite$sample <- "Candidate: White"
ind_interact_pooled <- rbind(ind_interact_genblack, ind_interact_genwhite)
ind_interact_pooled$sample <- factor(ind_interact_pooled$sample)




fig17_app <- ggplot(ind_interact_pooled, aes(x = level, y = estimate, linetype = cand_gender)) +
  geom_hline(yintercept = 0.5, colour = gray(0.7), lty = 1) + 
  geom_point(position=position_dodge(0.4)) +
  geom_errorbar(aes(x = level, 
                    ymin = lower,
                    ymax = upper,
                    linetype = cand_gender),
                width = .3,
                alpha = 0.8,
                position=position_dodge(0.4),
                lwd = 0.7) +
  facet_wrap(~sample) +
  coord_flip() +
  scale_y_continuous(labels = label_number(accuracy = 0.01),
                     breaks = c(0.45, 0.5, 0.55)) +
  scale_linetype_discrete(name="Candidate Gender",
                          labels =c("Female", "Male")) +
  labs(x = "Crime Policy",
       y = "Marginal Mean") +
  theme_bw() +
  theme(text = element_text(size = 16),
        axis.text.x = element_text(size = 14),
        axis.text.y = element_text(size = 14),
        legend.position = "bottom",
        legend.box.background = element_rect(colour = "black"))

fig17_app

#ggsave("app_ind_fig4a2_plot.png", fig17_app, width=8, height=3)




# Figure A18: Pooled Respondent Preferences for Crime Platforms Across Strong
############ and Weak Democrats

strongdem_model <- cj(strongdem_data,
                      chosen ~ cand_pubsec,
                      id = ~id,
                      estimate = "mm",
                      alpha = .1)

leandem_model <- cj(leandem_data,
                    chosen ~ cand_pubsec,
                    id = ~id,
                    estimate = "mm",
                    alpha = .1)


### combining
strongdem_model$sample <- "Strong Democrats"
leandem_model$sample <- "Weak Democrats"
dembase_pooled <- rbind(strongdem_model, leandem_model)
dembase_pooled$sample <- factor(dembase_pooled$sample, 
                                levels = c("Strong Democrats", "Weak Democrats"),
                                ordered = T)

### Appendix - Strength of Democrat ID
fig18_app <- ggplot(dembase_pooled, aes(x = level, y = estimate)) +
  geom_hline(yintercept = 0.5, colour = gray(0.7), lty = 1) + 
  geom_point(position=position_dodge(0.2)) +
  geom_errorbar(aes(x = level, 
                    ymin = lower,
                    ymax = upper),
                width = .3,
                alpha = 0.8,
                position=position_dodge(0.2),
                lwd = 0.7) + 
  facet_wrap(~sample) +
  coord_flip() +
  scale_y_continuous(labels = label_number(accuracy = 0.01),
                     breaks = c(0.46, 0.5, 0.54)) +
  labs(x = "Crime Policy",
       y = "Marginal Mean") +
  theme_bw() +
  theme(text = element_text(size = 16),
        axis.text.x = element_text(size = 14),
        axis.text.y = element_text(size = 14),
        legend.position = "bottom",
        legend.box.background = element_rect(colour = "black"),
        panel.spacing = unit(2, "lines"))

fig18_app 

#ggsave("app_demstrength_plot.png", fig18_app, width=8, height=3)




# Figure A19: Pooled Respondent Preferences for Crime Platforms Across Strong
############ and Weak Democrats by Candidate Race

strongdem_interact_race <- cj(strongdem_data,
                              chosen ~ cand_pubsec,
                              id = ~id,
                              estimate = "mm",
                              by = ~ cand_race,
                              alpha = .1)

leandem_interact_race <- cj(leandem_data,
                            chosen ~ cand_pubsec,
                            id = ~id,
                            estimate = "mm",
                            by = ~ cand_race,
                            alpha = .1)


### combining
strongdem_interact_race$sample <- "Strong Democrats"
leandem_interact_race$sample <- "Weak Democrats"
dem_race_pooled <- rbind(strongdem_interact_race, leandem_interact_race)
dem_race_pooled$sample <- factor(dem_race_pooled$sample, 
                                 levels = c("Strong Democrats", "Weak Democrats"),
                                 ordered = T)



fig19_app <- ggplot(dem_race_pooled, aes(x = level, y = estimate, linetype = cand_race)) +
  geom_hline(yintercept = 0.5, colour = gray(0.7), lty = 1) + 
  geom_point(position=position_dodge(0.2)) +
  geom_errorbar(aes(x = level, 
                    ymin = lower,
                    ymax = upper,
                    linetype = BY),
                width = .3,
                alpha = 0.8,
                position=position_dodge(0.2),
                lwd = 0.7) +
  facet_wrap(~sample) +
  coord_flip() +
  #scale_y_continuous(labels = label_number(accuracy = 0.01),
  #                 breaks = c(0.47, 0.5, 0.53)) +
  scale_linetype_discrete(name="Candidate Race",
                          labels =c("Black", "White")) +
  labs(x = "Crime Policy",
       y = "Marginal Mean") +
  theme_bw() +
  theme(text = element_text(size = 16),
        axis.text.x = element_text(size = 14),
        axis.text.y = element_text(size = 14),
        legend.position = "bottom",
        legend.box.background = element_rect(colour = "black"),
        panel.spacing = unit(2, "lines"))

fig19_app 

#ggsave("app_demstrength_fig5_plot.png", fig19_app , width=8, height=3)





# Figure A20: Pooled Respondent Preferences for Crime Platforms Across Strong
############ and Weak Democrats by Candidate Gender

strongdem_interact_gender <- cj(strongdem_data,
                                chosen ~ cand_pubsec,
                                id = ~id,
                                estimate = "mm",
                                by = ~ cand_gender,
                                alpha = .1)

leandem_interact_gender <- cj(leandem_data,
                              chosen ~ cand_pubsec,
                              id = ~id,
                              estimate = "mm",
                              by = ~ cand_gender,
                              alpha = .1)


### combining
strongdem_interact_gender$sample <- "Strong Democrats"
leandem_interact_gender$sample <- "Weak Democrats"
dem_gender_pooled <- rbind(strongdem_interact_gender, leandem_interact_gender)
dem_gender_pooled$sample <- factor(dem_gender_pooled$sample, 
                                   levels = c("Strong Democrats", "Weak Democrats"),
                                   ordered = T)



fig20_app <- ggplot(dem_gender_pooled, aes(x = level, y = estimate, linetype = cand_gender)) +
  geom_hline(yintercept = 0.5, colour = gray(0.7), lty = 1) + 
  geom_point(position=position_dodge(0.2)) +
  geom_errorbar(aes(x = level, 
                    ymin = lower,
                    ymax = upper,
                    linetype = BY),
                width = .3,
                alpha = 0.8,
                position=position_dodge(0.2),
                lwd = 0.7) +
  facet_wrap(~sample) +
  coord_flip() +
  scale_y_continuous(labels = label_number(accuracy = 0.01),
                     breaks = c(0.47, 0.5, 0.53)) +
  scale_linetype_discrete(name="Candidate Race",
                          labels =c("Black", "White")) +
  labs(x = "Crime Policy",
       y = "Marginal Mean") +
  theme_bw() +
  theme(text = element_text(size = 16),
        axis.text.x = element_text(size = 14),
        axis.text.y = element_text(size = 14),
        legend.position = "bottom",
        legend.box.background = element_rect(colour = "black"),
        panel.spacing = unit(2, "lines"))

fig20_app

#ggsave("app_demstrength_fig6_plot.png", fig20_app, width=8, height=3)





# Figure A21:  Preferences for Crime Platforms by Candidate Race and Gender 
############ Among Strong Democrats

black_strongdem_cands <- strongdem_data %>% filter(cand_race == "Black")
white_strongdem_cands <- strongdem_data %>% filter(cand_race == "White")

strongdem_interact_genblack <- cj(black_strongdem_cands,
                                  chosen ~ cand_pubsec,
                                  id = ~id,
                                  estimate = "mm",
                                  by = ~ cand_gender,
                                  alpha = .1)

strongdem_interact_genwhite <- cj(white_strongdem_cands,
                                  chosen ~ cand_pubsec,
                                  id = ~id,
                                  estimate = "mm",
                                  by = ~ cand_gender,
                                  alpha = .1)

### combining
strongdem_interact_genblack$sample <- "Candidate: Black"
strongdem_interact_genwhite$sample <- "Candidate: White"
strongdem_interact_pooled <- rbind(strongdem_interact_genblack, strongdem_interact_genwhite)
strongdem_interact_pooled$sample <- factor(strongdem_interact_pooled$sample)



fig21_app <- ggplot(strongdem_interact_pooled, aes(x = level, y = estimate, linetype = cand_gender)) +
  geom_hline(yintercept = 0.5, colour = gray(0.7), lty = 1) + 
  geom_point(position=position_dodge(0.4)) +
  geom_errorbar(aes(x = level, 
                    ymin = lower,
                    ymax = upper,
                    linetype = cand_gender),
                width = .3,
                alpha = 0.8,
                position=position_dodge(0.4),
                lwd = 0.7) +
  facet_wrap(~sample) +
  coord_flip() +
  scale_y_continuous(labels = label_number(accuracy = 0.01),
                     breaks = c(0.45, 0.5, 0.55)) +
  scale_linetype_discrete(name="Candidate Gender",
                          labels =c("Female", "Male")) +
  labs(x = "Crime Policy",
       y = "Marginal Mean") +
  theme_bw() +
  theme(text = element_text(size = 16),
        axis.text.x = element_text(size = 14),
        axis.text.y = element_text(size = 14),
        legend.position = "bottom",
        legend.box.background = element_rect(colour = "black"))

fig21_app

#ggsave("app_strongdem_fig4a2_plot.png", fig21_app, width=8, height=3)


# Figure A22:  Preferences for Crime Platforms by Candidate Race and Gender 
############ Among Weak Democrats

black_leandem_cands <- leandem_data %>% filter(cand_race == "Black")
white_leandem_cands <- leandem_data %>% filter(cand_race == "White")

leandem_interact_genblack <- cj(black_leandem_cands,
                                chosen ~ cand_pubsec,
                                id = ~id,
                                estimate = "mm",
                                by = ~ cand_gender,
                                alpha = .1)

leandem_interact_genwhite <- cj(white_leandem_cands,
                                chosen ~ cand_pubsec,
                                id = ~id,
                                estimate = "mm",
                                by = ~ cand_gender,
                                alpha = .1)

### combining
leandem_interact_genblack$sample <- "Candidate: Black"
leandem_interact_genwhite$sample <- "Candidate: White"
leandem_interact_pooled <- rbind(leandem_interact_genblack, leandem_interact_genwhite)
leandem_interact_pooled$sample <- factor(leandem_interact_pooled$sample)




fig22_app <- ggplot(leandem_interact_pooled, aes(x = level, y = estimate, linetype = cand_gender)) +
  geom_hline(yintercept = 0.5, colour = gray(0.7), lty = 1) + 
  geom_point(position=position_dodge(0.4)) +
  geom_errorbar(aes(x = level, 
                    ymin = lower,
                    ymax = upper,
                    linetype = cand_gender),
                width = .3,
                alpha = 0.8,
                position=position_dodge(0.4),
                lwd = 0.7) +
  facet_wrap(~sample) +
  coord_flip() +
  scale_y_continuous(labels = label_number(accuracy = 0.01),
                     breaks = c(0.45, 0.5, 0.55)) +
  scale_linetype_discrete(name="Candidate Gender",
                          labels =c("Female", "Male")) +
  labs(x = "Crime Policy",
       y = "Marginal Mean") +
  theme_bw() +
  theme(text = element_text(size = 16),
        axis.text.x = element_text(size = 14),
        axis.text.y = element_text(size = 14),
        legend.position = "bottom",
        legend.box.background = element_rect(colour = "black"))

fig22_app

#ggsave("app_leandem_fig4a2_plot.png", fig22_app, width=8, height=3)




# Figure A23:  Pooled Respondent Preferences for Crime Platforms by Candidate
############ Race and Gender

black_pool_cands <- data %>% filter(cand_race == "Black")
white_pool_cands <- data %>% filter(cand_race == "White")

pool_interact_genblack <- cj(black_pool_cands,
                             chosen ~ cand_pubsec,
                             id = ~id,
                             estimate = "mm",
                             by = ~ cand_gender,
                             alpha = .1)

pool_interact_genwhite <- cj(white_pool_cands,
                             chosen ~ cand_pubsec,
                             id = ~id,
                             estimate = "mm",
                             by = ~ cand_gender,
                             alpha = .1)

### combining
pool_interact_genblack$sample <- "Candidate: Black"
pool_interact_genwhite$sample <- "Candidate: White"
pool_interact_pooled <- rbind(pool_interact_genblack, pool_interact_genwhite)
pool_interact_pooled$sample <- factor(pool_interact_pooled$sample)




fig23a_app <- ggplot(pool_interact_pooled, aes(x = level, y = estimate, linetype = cand_gender)) +
  geom_hline(yintercept = 0.5, colour = gray(0.7), lty = 1) + 
  geom_point(position=position_dodge(0.4)) +
  geom_errorbar(aes(x = level, 
                    ymin = lower,
                    ymax = upper,
                    linetype = cand_gender),
                width = .3,
                alpha = 0.8,
                position=position_dodge(0.4),
                lwd = 0.7) +
  facet_wrap(~sample) +
  coord_flip() +
  # scale_y_continuous(labels = label_number(accuracy = 0.01),
  # breaks = c(0.48, 0.5, 0.52)) +
  scale_linetype_discrete(name="Candidate Gender",
                          labels =c("Female", "Male")) +
  labs(x = "Crime Policy",
       y = "Marginal Mean") +
  theme_bw() +
  theme(text = element_text(size = 16),
        axis.text.x = element_text(size = 14),
        axis.text.y = element_text(size = 14),
        legend.position = "bottom",
        legend.box.background = element_rect(colour = "black"))

fig23a_app

#ggsave("app_PoolInteractA_plot.png", fig23a_app, width=8, height=3)



# difference in marginal means

diff_pool_interact_genblack <- cj(black_pool_cands,
                                  chosen ~ cand_pubsec,
                                  id = ~id,
                                  estimate = "mm_diff",
                                  by = ~ cand_gender,
                                  alpha = .1)

diff_pool_interact_genwhite <- cj(white_pool_cands,
                                  chosen ~ cand_pubsec,
                                  id = ~id,
                                  estimate = "mm_diff",
                                  by = ~ cand_gender,
                                  alpha = .1)

### combining
diff_pool_interact_genblack$sample <- "Candidate: Black"
diff_pool_interact_genwhite$sample <- "Candidate: White"
diff_pool_interact_total <- rbind(diff_pool_interact_genblack,
                                  diff_pool_interact_genwhite)
diff_pool_interact_total$sample <- factor(diff_pool_interact_total$sample)



fig23b_app <- ggplot(diff_pool_interact_total, aes(x = level, y = estimate)) +
  geom_hline(yintercept = 0.0, colour = gray(0.7), lty = 1) + 
  geom_point(position=position_dodge(0.3)) +
  geom_errorbar(aes(x = level, 
                    ymin = lower,
                    ymax = upper),
                width = .1,
                alpha = 0.8,
                position=position_dodge(0.3),
                lwd = 0.7) +
  facet_grid(~sample) +
  coord_flip() +
  #scale_y_continuous(labels = label_number(accuracy = 0.01),
  # breaks = c(-0.04, 0.00, 0.04)) +
  labs(x = "Crime Policy",
       y = "Difference in Marginal Mean") +
  theme_bw() +
  theme(text = element_text(size = 16),
        axis.text.x = element_text(size = 14),
        axis.text.y = element_text(size = 14))

fig23b_app

#ggsave("app_PoolInteractB_plot.png", fig23a_app, width=8, height=2)





# Figure A24:  Republican Respondent Preferences for Crime Platforms by Candidate
############ Race and Gender

black_rep_cands <- rep_data %>% filter(cand_race == "Black")
white_rep_cands <- rep_data %>% filter(cand_race == "White")

rep_interact_genblack <- cj(black_rep_cands,
                            chosen ~ cand_pubsec,
                            id = ~id,
                            estimate = "mm",
                            by = ~ cand_gender,
                            alpha = .1)

rep_interact_genwhite <- cj(white_rep_cands,
                            chosen ~ cand_pubsec,
                            id = ~id,
                            estimate = "mm",
                            by = ~ cand_gender,
                            alpha = .1)

### combining
rep_interact_genblack$sample <- "Candidate: Black"
rep_interact_genwhite$sample <- "Candidate: White"
rep_interact_pooled <- rbind(rep_interact_genblack, rep_interact_genwhite)
rep_interact_pooled$sample <- factor(rep_interact_pooled$sample)




fig24a_app <- ggplot(rep_interact_pooled, aes(x = level, y = estimate, linetype = cand_gender)) +
  geom_hline(yintercept = 0.5, colour = gray(0.7), lty = 1) + 
  geom_point(position=position_dodge(0.4)) +
  geom_errorbar(aes(x = level, 
                    ymin = lower,
                    ymax = upper,
                    linetype = cand_gender),
                width = .3,
                alpha = 0.8,
                position=position_dodge(0.4),
                lwd = 0.7) +
  facet_wrap(~sample) +
  coord_flip() +
  scale_y_continuous(labels = label_number(accuracy = 0.01),
                     breaks = c(0.45, 0.5, 0.55)) +
  scale_linetype_discrete(name="Candidate Gender",
                          labels =c("Female", "Male")) +
  labs(x = "Crime Policy",
       y = "Marginal Mean") +
  theme_bw() +
  theme(text = element_text(size = 16),
        axis.text.x = element_text(size = 14),
        axis.text.y = element_text(size = 14),
        legend.position = "bottom",
        legend.box.background = element_rect(colour = "black"))

fig24a_app

#ggsave("app_RepInteractA_plot.png", fig24a_app, width=8, height=3)



# difference in marginal means

diff_rep_interact_genblack <- cj(black_rep_cands,
                                 chosen ~ cand_pubsec,
                                 id = ~id,
                                 estimate = "mm_diff",
                                 by = ~ cand_gender,
                                 alpha = .1)

diff_rep_interact_genwhite <- cj(white_rep_cands,
                                 chosen ~ cand_pubsec,
                                 id = ~id,
                                 estimate = "mm_diff",
                                 by = ~ cand_gender,
                                 alpha = .1)

### combining
diff_rep_interact_genblack$sample <- "Candidate: Black"
diff_rep_interact_genwhite$sample <- "Candidate: White"
diff_rep_interact_total <- rbind(diff_rep_interact_genblack,
                                 diff_rep_interact_genwhite)
diff_rep_interact_total$sample <- factor(diff_rep_interact_total$sample)



fig24b_app <- ggplot(diff_rep_interact_total, aes(x = level, y = estimate)) +
  geom_hline(yintercept = 0.0, colour = gray(0.7), lty = 1) + 
  geom_point(position=position_dodge(0.3)) +
  geom_errorbar(aes(x = level, 
                    ymin = lower,
                    ymax = upper),
                width = .1,
                alpha = 0.8,
                position=position_dodge(0.3),
                lwd = 0.7) +
  facet_grid(~sample) +
  coord_flip() +
  scale_y_continuous(labels = label_number(accuracy = 0.01),
                     breaks = c(-0.04, 0.00, 0.04)) +
  labs(x = "Crime Policy",
       y = "Difference in Marginal Mean") +
  theme_bw() +
  theme(text = element_text(size = 16),
        axis.text.x = element_text(size = 14),
        axis.text.y = element_text(size = 14))

fig24b_app

#ggsave("app_RepInteractB_plot.png", fig24b_app, width=8, height=2)


# Figure A25: Democratic Respondent Preferences fro All Policy Attributes by
############ Candidate Race and Gender

black_dem_cands <- dem_data %>% filter(cand_race == "Black")
white_dem_cands <- dem_data %>% filter(cand_race == "White")

dem_interact_genblack <- cj(black_dem_cands,
                            chosen ~ cand_tax +
                              cand_pubsec + cand_edu,
                            id = ~id,
                            estimate = "mm",
                            by = ~ cand_gender,
                            alpha = .1)

dem_interact_genwhite <- cj(white_dem_cands,
                            chosen ~ cand_tax +
                              cand_pubsec + cand_edu,
                            id = ~id,
                            estimate = "mm",
                            by = ~ cand_gender,
                            alpha = .1)

### combining
dem_interact_genblack$sample <- "Candidate: Black"
dem_interact_genwhite$sample <- "Candidate: White"
dem_interact_pooled <- rbind(dem_interact_genblack, dem_interact_genwhite)
dem_interact_pooled$sample <- factor(dem_interact_pooled$sample)


#re-label features for plot
dem_interact_pooled$feature <- ifelse(dem_interact_pooled$feature == "cand_gender", "Candidate Gender",
                                      ifelse(dem_interact_pooled$feature == "cand_race", "Candidate Race",
                                             ifelse(dem_interact_pooled$feature == "cand_tax", "Tax Policy",
                                                    ifelse(dem_interact_pooled$feature == "cand_pubsec", "Public Security Policy",
                                                           ifelse(dem_interact_pooled$feature == "cand_edu", "Education Policy", NA)))))


# Plot - race gender interaction for all features

fig25_app <- ggplot(dem_interact_pooled, aes(x = level, y = estimate, linetype = cand_gender,
                                                    color = feature)) +
  geom_hline(yintercept = 0.5, colour = gray(0.7), lty = 1) + 
  geom_point(position=position_dodge(0.4)) +
  geom_errorbar(aes(x = level, 
                    ymin = lower,
                    ymax = upper,
                    linetype = cand_gender),
                width = .3,
                alpha = 0.8,
                position=position_dodge(0.4),
                lwd = 0.7) +
  facet_wrap(~sample) +
  coord_flip() +
  scale_linetype_discrete(name="Candidate Gender",
                          labels =c("Female", "Male")) +
  labs(x = "Policy",
       y = "Marginal Mean",
       color = "Attribute") +
  theme_bw() +
  theme(text = element_text(size = 16),
        axis.text.x = element_text(size = 12),
        axis.text.y = element_text(size = 14),
        # legend.position = "bottom",
        legend.box.background = element_rect(colour = "black"),
        panel.spacing = unit(2, "lines"))


fig25_app

#ggsave("app_racegen_interact_plot.png", fig25_app, width=12, height=5)


# Figure A26: AMCE: Preferences for Crime Platforms by Respondent Partisanship

base_model <- cj(data,
                 chosen ~ cand_pubsec,
                 id = ~id,
                 estimate = "amce",
                 alpha = .1)

dem_model <- cj(dem_data,
                chosen ~ cand_pubsec,
                id = ~id,
                estimate = "amce",
                alpha = .1)

rep_model <- cj(rep_data,
                chosen ~ cand_pubsec,
                id = ~id,
                estimate = "amce",
                alpha = .1)


### combining
base_model$sample <- "Pooled"
dem_model$sample <- "Democrats"
rep_model$sample <- "Republicans"
base_pooled <- rbind(base_model, dem_model, rep_model)
base_pooled$sample <- factor(base_pooled$sample, 
                             levels = c("Pooled", "Democrats",
                                        "Republicans"),
                             ordered = T)



fig26_app <- ggplot(base_pooled, aes(x = level, y = estimate)) +
  geom_hline(yintercept = 0, colour = gray(0.7), lty = 1) + 
  geom_point(position=position_dodge(0.2)) +
  geom_errorbar(aes(x = level, 
                    ymin = lower,
                    ymax = upper),
                width = .3,
                alpha = 0.8,
                position=position_dodge(0.2),
                lwd = 0.7) +
  facet_wrap(~sample) +
  coord_flip() +
  scale_y_continuous(labels = label_number(accuracy = 0.01),
                     breaks = c(-0.05, 0, 0.05)) +
  labs(x = "Crime Policy",
       y = "Average Marginal Component Effect (AMCE)") +
  theme_bw() +
  theme(text = element_text(size = 16),
        axis.text.x = element_text(size = 14),
        axis.text.y = element_text(size = 14),
        legend.position = "bottom",
        legend.box.background = element_rect(colour = "black"))

fig26_app


#ggsave("app_fig1_plot.png", fig26_app, width=8, height=3)


# Figure A27: AMCE: Preferences for Crime Platforms by Respondent Partisanship
############ and Candidate Race

base_interact_race <- cj(data,
                         chosen ~ cand_pubsec,
                         id = ~id,
                         estimate = "amce",
                         by = ~ cand_race,
                         alpha = .1)

dem_interact_race <- cj(dem_data,
                        chosen ~ cand_pubsec,
                        id = ~id,
                        estimate = "amce",
                        by = ~ cand_race,
                        alpha = .1)


rep_interact_race <- cj(rep_data,
                        chosen ~ cand_pubsec,
                        id = ~id,
                        estimate = "amce",
                        by = ~ cand_race,
                        alpha = .1)

### combining
base_interact_race$sample <- "Pooled"
dem_interact_race$sample <- "Democrats"
rep_interact_race$sample <- "Republicans"
race_pooled <- rbind(base_interact_race, dem_interact_race,
                     rep_interact_race)
race_pooled$sample <- factor(race_pooled$sample, 
                             levels = c("Pooled", "Democrats",
                                        "Republicans"),
                             ordered = T)



fig27_app <- ggplot(race_pooled, aes(x = level, y = estimate, linetype = cand_race)) +
  geom_hline(yintercept = 0, colour = gray(0.7), lty = 1) + 
  geom_point(position=position_dodge(0.2)) +
  geom_errorbar(aes(x = level, 
                    ymin = lower,
                    ymax = upper,
                    linetype = BY),
                width = .3,
                alpha = 0.8,
                position=position_dodge(0.2),
                lwd = 0.7) +
  facet_wrap(~sample) +
  coord_flip() +
  scale_y_continuous(labels = label_number(accuracy = 0.01),
                     breaks = c(-0.05, 0, 0.05)) +
  scale_linetype_discrete(name="Candidate Race",
                          labels =c("Black", "White")) +
  labs(x = "Crime Policy",
       y = "Average Marginal Component Effect (AMCE)") +
  theme_bw() +
  theme(text = element_text(size = 16),
        axis.text.x = element_text(size = 14),
        axis.text.y = element_text(size = 14),
        legend.position = "bottom",
        legend.box.background = element_rect(colour = "black"))

fig27_app


#ggsave("app_fig2a_plot.png", fig27_app, width=8, height=3)



# Figure A28: AMCE: Preferences for Crime Platforms by Respondent Partisanship
############ and Candidate Gender


base_interact_gender <- cj(data,
                           chosen ~ cand_pubsec,
                           id = ~id,
                           estimate = "amce",
                           by = ~ cand_gender,
                           alpha = .1)


dem_interact_gender <- cj(dem_data,
                          chosen ~ cand_pubsec,
                          id = ~id,
                          estimate = "amce",
                          by = ~ cand_gender,
                          alpha = .1)


rep_interact_gender <- cj(rep_data,
                          chosen ~ cand_pubsec,
                          id = ~id,
                          estimate = "amce",
                          by = ~ cand_gender,
                          alpha = .1)

### combining
base_interact_gender$sample <- "Pooled"
dem_interact_gender$sample <- "Democrats"
rep_interact_gender$sample <- "Republicans"
gender_pooled <- rbind(base_interact_gender, dem_interact_gender,
                       rep_interact_gender)
gender_pooled$sample <- factor(gender_pooled$sample, 
                               levels = c("Pooled", "Democrats",
                                          "Republicans"),
                               ordered = T)




fig28_app <- ggplot(gender_pooled, aes(x = level, y = estimate, linetype = cand_gender)) +
  geom_hline(yintercept = 0, colour = gray(0.7), lty = 1) + 
  geom_point(position=position_dodge(0.2)) +
  geom_errorbar(aes(x = level, 
                    ymin = lower,
                    ymax = upper,
                    linetype = BY),
                width = .3,
                alpha = 0.8,
                position=position_dodge(0.2),
                lwd = 0.7) +
  facet_wrap(~sample) +
  coord_flip() +
  scale_y_continuous(labels = label_number(accuracy = 0.01),
                     breaks = c(-0.05, 0, 0.05)) +
  scale_linetype_discrete(name="Candidate Gender",
                          labels =c("Female", "Male")) +
  labs(x = "Crime Policy",
       y = "Average Marginal Component Effect (AMCE)") +
  theme_bw() +
  theme(text = element_text(size = 16),
        axis.text.x = element_text(size = 14),
        axis.text.y = element_text(size = 14),
        legend.position = "bottom",
        legend.box.background = element_rect(colour = "black"))

fig28_app


#ggsave("app_fig3a_plot.png", fig28_app, width=8, height=3)




# Figure A29: AMCE: Democratic Preferences for Crime Platforms by Candidate 
############ Race and Gender

black_dem_cands <- dem_data %>% filter(cand_race == "Black")
white_dem_cands <- dem_data %>% filter(cand_race == "White")

dem_interact_genblack <- cj(black_dem_cands,
                            chosen ~ cand_pubsec,
                            id = ~id,
                            estimate = "amce",
                            by = ~ cand_gender,
                            alpha = .1)

dem_interact_genwhite <- cj(white_dem_cands,
                            chosen ~ cand_pubsec,
                            id = ~id,
                            estimate = "amce",
                            by = ~ cand_gender,
                            alpha = .1)

### combining
dem_interact_genblack$sample <- "Candidate: Black"
dem_interact_genwhite$sample <- "Candidate: White"
dem_interact_pooled <- rbind(dem_interact_genblack, dem_interact_genwhite)
dem_interact_pooled$sample <- factor(dem_interact_pooled$sample)




fig29_app <- ggplot(dem_interact_pooled, aes(x = level, y = estimate, linetype = cand_gender)) +
  geom_hline(yintercept = 0, colour = gray(0.7), lty = 1) + 
  geom_point(position=position_dodge(0.4)) +
  geom_errorbar(aes(x = level, 
                    ymin = lower,
                    ymax = upper,
                    linetype = cand_gender),
                width = .3,
                alpha = 0.8,
                position=position_dodge(0.4),
                lwd = 0.7) +
  facet_wrap(~sample) +
  coord_flip() +
  scale_y_continuous(labels = label_number(accuracy = 0.01),
                     breaks = c(-0.05, 0.0, 0.05)) +
  scale_linetype_discrete(name="Candidate Gender",
                          labels =c("Female", "Male")) +
  labs(x = "Crime Policy",
       y = "Average Marginal Component Effect (AMCE)") +
  theme_bw() +
  theme(text = element_text(size = 16),
        axis.text.x = element_text(size = 14),
        axis.text.y = element_text(size = 14),
        legend.position = "bottom",
        legend.box.background = element_rect(colour = "black"))

fig29_app


#ggsave("app_fig4_plot.png", fig29_app, width=8, height=3)



# Figure A30: Preferences for Crime Platforms by Candidate Race and 
############ Perceptions of Tough-on-Crime Policy

# All respondents
pool_target <- cj(pooled_data_target,
                  chosen ~ cand_pubsec,
                  id = ~id,
                  estimate = "mm",
                  by = ~ cand_race,
                  alpha = .1)

pool_notarget <- cj(pooled_data_notarget,
                    chosen ~ cand_pubsec,
                    id = ~id,
                    estimate = "mm",
                    by = ~ cand_race,
                    alpha = .1)


### combining
pool_target$sample <- "Target"
pool_notarget$sample <- "Do Not Target"
target_pooled <- rbind(pool_target, pool_notarget)
target_pooled$sample <- factor(target_pooled$sample, 
                               levels = c("Target", "Do Not Target"),
                               ordered = T)



# Figure 
fig30_app <- ggplot(target_pooled, aes(x = level, y = estimate, linetype = cand_race)) +
  geom_hline(yintercept = 0.5, colour = gray(0.7), lty = 1) + 
  geom_point(position=position_dodge(0.2)) +
  geom_errorbar(aes(x = level, 
                    ymin = lower,
                    ymax = upper,
                    linetype = BY),
                width = .3,
                alpha = 0.8,
                position=position_dodge(0.2),
                lwd = 0.7) +
  facet_wrap(~sample) +
  coord_flip() +
  scale_linetype_discrete(name="Candidate Race",
                          labels =c("Black", "White")) +
  labs(x = "Crime Policy",
       y = "Marginal Mean") +
  theme_bw() +
  theme(text = element_text(size = 16),
        axis.text.x = element_text(size = 14),
        axis.text.y = element_text(size = 14),
        legend.position = "bottom",
        legend.box.background = element_rect(colour = "black"),
        panel.spacing = unit(1, "lines"))

fig30_app

#ggsave("app_toughtarget_pooled.png", fig30_app, width=8, height=3)




# Figure A31: Democrat Preferences for Crime Platforms by Candidate Race and 
############ Perceptions of Tough-on-Crime Policy


dem_target <- cj(dem_data_target,
                 chosen ~ cand_pubsec,
                 id = ~id,
                 estimate = "mm",
                 by = ~ cand_race,
                 alpha = .1)

dem_notarget <- cj(dem_data_notarget,
                   chosen ~ cand_pubsec,
                   id = ~id,
                   estimate = "mm",
                   by = ~ cand_race,
                   alpha = .1)


### combining
dem_target$sample <- "Target"
dem_notarget$sample <- "Do Not Target"
target_pooled <- rbind(dem_target, dem_notarget)
target_pooled$sample <- factor(target_pooled$sample, 
                               levels = c("Target", "Do Not Target"),
                               ordered = T)



# Figure 
fig31_app <- ggplot(target_pooled, aes(x = level, y = estimate, linetype = cand_race)) +
  geom_hline(yintercept = 0.5, colour = gray(0.7), lty = 1) + 
  geom_point(position=position_dodge(0.2)) +
  geom_errorbar(aes(x = level, 
                    ymin = lower,
                    ymax = upper,
                    linetype = BY),
                width = .3,
                alpha = 0.8,
                position=position_dodge(0.2),
                lwd = 0.7) +
  facet_wrap(~sample) +
  coord_flip() +
  #scale_y_continuous(labels = label_number(accuracy = 0.01),
  # breaks = c(0.475, 0.5, 0.525)) +
  scale_linetype_discrete(name="Candidate Race",
                          labels =c("Black", "White")) +
  labs(x = "Crime Policy",
       y = "Marginal Mean") +
  theme_bw() +
  theme(text = element_text(size = 16),
        axis.text.x = element_text(size = 14),
        axis.text.y = element_text(size = 14),
        legend.position = "bottom",
        legend.box.background = element_rect(colour = "black"),
        panel.spacing = unit(1, "lines"))

fig31_app

#ggsave("app_toughtarget_dem.png", fig31_app, width=8, height=3)




